//
//  BIDViewController.m
//  Control Fun
//
//  Created by quentin on 15/10/28.
//  Copyright (c) 2015年 QuentinLabs. All rights reserved.
//

#import "BIDViewController.h"

@interface BIDViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *numberField;

- (IBAction)sliderChanged:(UISlider *)sender;
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
@property (weak, nonatomic) IBOutlet UISwitch *leftSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *rightSwitch;
- (IBAction)switchChanged:(UISwitch *)sender;
- (IBAction)toggleControls:(UISegmentedControl *)sender;
@property (weak, nonatomic) IBOutlet UIButton *doSomethingButton;

@end

@implementation BIDViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.sliderLabel.text = @"50";
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)textFieldDoneEditing:(id)sender
{
    [sender resignFirstResponder];
}

-(IBAction)backgroundTap:(id)sender{
    [self.nameField resignFirstResponder];
    [self.numberField resignFirstResponder];
}

- (IBAction)sliderChanged:(UISlider *)sender {
    int progress = lroundf(sender.value);
    self.sliderLabel.text = [NSString stringWithFormat:@"%d",progress];
}
- (IBAction)switchChanged:(UISwitch *)sender {
    BOOL setting = sender.isOn;
    [self.leftSwitch setOn:setting animated:YES];
    [self.rightSwitch setOn:setting animated:YES];
}

- (IBAction)toggleControls:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex ==0) {
        self.leftSwitch.hidden = NO;
        self.rightSwitch.hidden = NO;
        self.doSomethingButton.hidden = YES;
    }
    else{
        self.leftSwitch.hidden = YES;
        self.rightSwitch.hidden = YES;
        self.doSomethingButton.hidden = NO;
    }
        
}
- (IBAction)buttonPressed:(UIButton *)sender {
    
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"Are you sure"
                                  delegate:self
                                  cancelButtonTitle:@"No Way"
                                  destructiveButtonTitle:@"Yes,I'm Sure"
                                  otherButtonTitles:@"Foo",@"Bar",nil];
    [actionSheet showInView:self.view];
    }

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSString* msg = nil;
        if ([self.nameField.text length ] >0) {
            msg = [NSString stringWithFormat:
                   @"You can breathe easy,%@,everythin went OK.",
                   self.nameField.text];
        }else{
            msg = @"You can breathe easy,everything went OK.";
        }
        
        UIAlertView* alert = [[UIAlertView alloc]
                              initWithTitle:@"Somthing was done"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"phew!"
                              otherButtonTitles:nil];
        [alert show];
    }
}
@end
